<?php
$adm_menu = "1";

include("config.php");
if (!isset($_SESSION["auser"]) || $_SESSION["auser"] == "" || $_SESSION['agroup'] != "WEB_ADMIN") header('Location: index.php');


$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);
if (!$db_rdata->isConnected()) {	
	die("Klientų aptarnavimo sistema laikinai neveikia(rdata).");
}	

include("header.php");
include("menu.php");

?>
<script type="text/javascript">
function changeTime(degid,val) {
	rez = prompt('Įveskite vėlavimo valandų skaičių',val);
	if (rez != null) {
		var intRegex = /^\d+$/;
		if (!intRegex.test(rez)) {
			alert('Neteisingas valandų skaičius!');
		} else {
			window.location='overview.php?degid='+degid+'&val='+rez;
		}
	}
}
</script>
<?php
if (isset($_REQUEST["degid"]) && isset($_REQUEST["val"])) {
	if (is_numeric($_REQUEST["degid"]) && is_numeric($_REQUEST["val"])) {
		$id = intval($_REQUEST["degid"]);
		$v = intval($_REQUEST["val"]);
		$q = "exec web_upd_aparat_velav ".$id.",".$v;
		$rimone = $db_rdata->execute($q);
		echo "Informacija atnaujinta";		
	}
}
echo "<table class='innercont' cellspacing='0' cellpadding='10' border='0'>";
echo "<tr><td class='leftcol2'>";
if ($kreditas == "1") {
	echo "<table cellspacing=\"0\" cellpadding=\"5\" class=\"params\" style=\"\"><tbody><tr><td style=\"text-align:center;\"><a id=\"editButton\" href=\"vouchers.php\" class=\"card3\">Kvitai</a></td></tr></tbody></table><br>";
}
echo "<table cellspacing=\"0\" cellpadding=\"5\" class=\"params\" style=\"\"><tbody><tr><td style=\"text-align:center;\"><a id=\"editButton\" href=\"terminals.php\" class=\"card3\">Aparatai</a></td></tr></tbody></table>";
echo "</td>";
echo "<td class='centercol'>";

	$ns = $db_rdata->returnList($overview_query);
	echo '<table class="innercont2" cellspacing="0" cellpadding="2"><tr><th>Deg. Id</th><th>Pavadinimas</th><th>Zeto Nr.</th><th>Suma</th><th>Nuolaida</th><th>Korteles Nr.</th><th>Kvito id</th><th>Data Laikas</th></tr>';
	foreach ($ns as $degaline) {
		
		if ($degaline["id"] != null) {
		
			$q = str_replace("<KvitoId>", $degaline["id"], $overview_det_query);
			$inf = $db_rdata->returnInfo($q);
		
			$zeto = $inf["Znr"];
			$suma = sprintf("%01.2f",$inf["Suma"]);
			$nuolaida = $inf["Nuolaida"];
			$kort = $inf["KortelesNr"];
			$data = $inf["data"];
			$color = $inf["color"];
			if ($nuolaida == 0) {
				$nuolaida = "";
			} else {
				$nuolaida = sprintf("%01.2f",$nuolaida);
			}
			if ($degaline["velavimas"] == null) {
				$velavimas = 0;
			} else {
				$velavimas = $degaline["velavimas"];
			}
			
		} else {
			$zeto = "";
			$suma = "";
			$nuolaida = "";
			$kort = "";
			$data = "";
			$color = "white";
			$velavimas = 0;
		}
	
	
		echo "<tr>";
		echo "<td style='text-align:right;'>".$degaline["AparatoID"]."</td>";
		echo "<td>".@iconv('windows-1257','UTF-8',$degaline["AparatoPavadinimas"])."</td>";
		echo "<td style='text-align:right;'>".$zeto."</td>";
		echo "<td style='text-align:right;'>".$suma."</td>";
		echo "<td style='text-align:right;'>".$nuolaida."</td>";
		echo "<td>".$kort."</td>";
		echo "<td style='text-align:right;'>".$degaline["id"]."</td>";
		echo "<td>".$data." <span style='color:".$color.";'>♦</span>";
		echo " <A href='javascript:changeTime(\"".$degaline["AparatoID"]."\",\"".$velavimas."\")'><img src='img/time.png' width='11' height='11' alt='".$velavimas."' title='".$velavimas."'></A>";
		echo"</td>";
		echo "</tr>";
	}
	echo "</table>";
	

echo '<table class="innercont2" cellspacing="0" cellpadding="5"><tr><td style="font-weight:bold;color: #959595;text-align:right;">';

	echo "<br>" . getVersionInfo();
	echo "<br>" . getRobotukasInfo();	

	echo "</td></tr></table>";
	
	echo "</td></tr></table>";

include("footer.php");
?>